package com.quyou.funtour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyou.funtour.bean.ResultInfo;
import com.quyou.funtour.dao.ActivityDao;
import com.quyou.funtour.dao.ActivityImgDao;
import com.quyou.funtour.dao.RegionDao;
import com.quyou.funtour.dao.UserDao;
import com.quyou.funtour.enummmeration.ActivityStatus;
import com.quyou.funtour.po.*;
import com.quyou.funtour.service.ActivityService;
import com.quyou.funtour.service.HistoryService;
import com.quyou.funtour.service.UserParticipateActivityService;
import com.quyou.funtour.vo.ActivityVo;
import com.quyou.funtour.vo.RegionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, ActivityPo> implements ActivityService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private UserParticipateActivityService userParticipateActivityService;
    @Autowired
    private ActivityImgDao activityImgDao;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RegionDao regionDao;

    /**
     * 根据活动id获取活动详情
     * @param id
     * @return
     */
    @Override
    public ActivityPo getActivityById(String id) {
        //获取活动
        ActivityPo activityPo = getById(id);
        if(activityPo==null){
            return null;
        }
        //获取参与该活动的用户头像
        if(activityPo!=null){
            List<String> userHeadPicByAid = userDao.getUserHeadPicByAid(activityPo.getId());
            activityPo.setUserPics(userHeadPicByAid);
        }
        //获取活动图片
        List<String> imgByAid = activityImgDao.getImgByAid(activityPo.getId());
        activityPo.setActivityPics(imgByAid);
        //为用户设置浏览记录
        /*if(userPo!=null && activityPo!=null){
            //获取用户是否已经浏览过此活动
            HistoryPo one = historyService
                    .getOne(new QueryWrapper<HistoryPo>()
                            .eq("a_id", activityPo.getId())
                            .eq("u_id", userPo.getId()));
            if(one!=null){
                //已经浏览就修改浏览时间
                one.setCreateTime(new Date());
                historyService.updateById(one);
            }else {
                //否则就添加新的浏览记录
                HistoryPo historyPo = new HistoryPo();
                historyPo.setAid(activityPo.getId());
                historyPo.setUid(userPo.getId());
                historyPo.setCreateTime(new Date());
                historyService.save(historyPo);
            }

        }*/

        return activityPo;
    }

    /**
     * 用户预约活动
     * @param yuyue
     * @return
     */
    @Override
    @Transactional
    public ResultInfo makeAnAppointment(UserParticipateActivityPo yuyue,UserPo userPo) {

        //获取活动
        ActivityPo activity = activityService.getById(yuyue.getAid());
        if(activity==null){
            return ResultInfo.ok("未找到该活动");
        }
        //判断时间是否在预约范围
        Date orderTime = activity.getOrderTime();
        if(orderTime.getTime()<new Date().getTime()){
            //预约时间已过
            return ResultInfo.no("预约时间已过，请练习管理员");
        }

        //判断活动状态
        ActivityStatus status = activity.getStatus();
        if(status.equals(ActivityStatus.OVER)){
            return ResultInfo.no("该活动已经结束");
        }

        //获取预约该活动的人
        List<UserParticipateActivityPo> list = userParticipateActivityService
                .list(new QueryWrapper<UserParticipateActivityPo>()
                        .eq("a_id",activity.getId()));
        if(list.size()>=activity.getPeopleNum()){
            //参与活动人数大于等于活动总人数
            return ResultInfo.no("当前活动太火爆，人数已满");
        }
        //判断用户是否已经预约
        UserParticipateActivityPo one = userParticipateActivityService
                .getOne(new QueryWrapper<UserParticipateActivityPo>().eq("a_id",activity.getId())
                        .eq("u_id",yuyue.getUid()));
        if(one!=null){
            return ResultInfo.no("您已经预约了此活动！！");
        }

        //预约入库
        yuyue.setCreateTime(new Date());
        yuyue.setUid(userPo.getId());
        boolean save = userParticipateActivityService.save(yuyue);
        if(save){
            return ResultInfo.ok("恭喜，预约成功！");
        }else {
            return ResultInfo.no("预约失败");
        }

    }

    /**
     * 根据用户id获取用户预约的所有活动
     * @param uid
     * @return
     */
    @Override
    public List<ActivityVo> getyuyueIngActivity(String uid) {
        //获取用户所预约的活动
        List<ActivityPo> yuYueActivityByUid = activityDao.getYuYueActivityByUid(uid);
        //克隆属性
        List<ActivityVo> activityPos = new ArrayList<>();
        for (ActivityPo activityPo : yuYueActivityByUid) {
            if(activityPo.getEndTime().getTime()<new Date().getTime()){
                continue;
            }
            ActivityVo activityVo = new ActivityVo();
            BeanUtils.copyProperties(activityPo,activityVo);
            List<UserParticipateActivityPo> a_id = userParticipateActivityService.list(new QueryWrapper<UserParticipateActivityPo>().eq("a_id", activityPo.getId()));
            activityVo.setAlreadyPeopleNum(a_id.size());
            activityVo.setEndTime(activityPo.getStartTime());
            activityPos.add(activityVo);
        }
        return activityPos;
    }

    /**
     * 根据用户id获取用户参与过的活动
     * @param uid
     * @return
     */
    @Override
    public List<ActivityVo> getParticipateActivity(String uid) {
        List<ActivityPo> participateActivity = activityDao.getParticipateActivity(uid);
        //克隆属性
        List<ActivityVo> activityPos = new ArrayList<>();
        for (ActivityPo activityPo : participateActivity) {

            ActivityVo activityVo = new ActivityVo();
            BeanUtils.copyProperties(activityPo,activityVo);
            List<UserParticipateActivityPo> a_id = userParticipateActivityService.list(new QueryWrapper<UserParticipateActivityPo>().eq("a_id", activityPo.getId()));
            activityVo.setAlreadyPeopleNum(a_id.size());
            activityVo.setEndTime(activityPo.getStartTime());
            activityPos.add(activityVo);
        }
        return activityPos;
    }

    /**
     * 获取所有活动详细信息
     * @param current 当前页
     * @param size 每页显示条数
     * @return
     */
    @Override
    public Page<ActivityPo> ctivityList(Integer current, Integer size) {
        Page<ActivityPo> activityPoPage = new Page<>(current,size);
        Page<ActivityPo> page = activityService.page(activityPoPage);
        for (ActivityPo record : page.getRecords()) {
            RegionPo nameById = regionDao.getNameById(record.getRid());
            RegionVo regionVo = new RegionVo();
            BeanUtils.copyProperties(nameById,regionVo);
            record.setRname(regionVo);
        }
        return page;
    }

    /**
     * 根据活动id获取活动图片
     * @param id
     * @return
     */
    @Override
    public List<String> getActivityImgById(String id) {
        return activityDao.getActivityImgById(id);
    }

    /**
     * 添加活动
     * @param activityPo
     * @return
     */
    @Override
    public Boolean addActivity(ActivityPo activityPo) {
        Date endTime = activityPo.getEndTime();
        Date orderTime = activityPo.getOrderTime();
        if(endTime.getTime()<new Date().getTime()){
            //活动已经结束
            //结束时间小于当前时间
            activityPo.setStatus(ActivityStatus.OVER);
        }else if(orderTime.getTime()<new Date().getTime()){
            //预约已经结束
            //预约时间小于当前时间
            activityPo.setStatus(ActivityStatus.UNDERWAY);
        }else {
            activityPo.setStatus(ActivityStatus.PROGRESSING);
        }
        boolean save = save(activityPo);
        return save;
    }
}
